(function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s);
    js.id = id;
    js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
    var _sf_async_config = { uid:39469, domain:'blackmilkclothing.com' };
    (function () {
        function loadChartbeat() {
            window._sf_endpt = (new Date()).getTime();
            var e = document.createElement('script');
            e.setAttribute('language', 'javascript');
            e.setAttribute('type', 'text/javascript');
            e.setAttribute('src',
                    (("https:" == document.location.protocol) ? "https://a248.e.akamai.net/chartbeat.download.akamai.com/102508/" : "http://static.chartbeat.com/") +
                            "js/chartbeat.js");
            document.body.appendChild(e);
        }

        ;
        var oldonload = window.onload;
        window.onload = (typeof window.onload != 'function') ?
                loadChartbeat : function () {
            oldonload();
            loadChartbeat();
        };
    })();

$("#product-tabs ul").idTabs();
        $(document).ready(function() {
            $('#wrap').css('z-index','999');
            $('#add-item-form').submit(function() {
                if (!$("input[name='id']:checked").val()) {
                    alert('You need to select a size!');
                    return false; // cancel form submission if passwords don't match
                }
                else {
                    return true; // return true if no errors
                }
            });
            
if (typeof featuredtag!='undefined') {
       featurethumbs(featuredtag);
}
       
       jQuery('.selected-currency').text(Currency.currentCurrency);
                   $('.best-selling > li').pick(7).each(function(i) {
                if ((i+1)%7 === 0) {
                    $(this).addClass('last');
                } else {
                    $(this).removeClass('last');
                }
            });
       });


function roundToHalf(a){var b=parseFloat(a);var c=b-parseInt(b,10);c=Math.round(c*10);if(c==5){return parseInt(b,10)+.5}if(c<3||c>7){return Math.round(b)}else{return parseInt(b,10)+.5}}function frontslider(a){var b="http://blackmilkclothing.com.s141641.gridserver.com/latest-tagjs.php?&callback=?";$.getJSON(b,function(a){$("#latest-tags").html(a.html);$("a.fbox").fancybox()})}function featurethumbs(a){var b="http://blackmilkclothing.com.s141641.gridserver.com/tagjs.php?tag="+a+"&callback=?";$.getJSON(b,function(b){$("#facebooktags-"+a).html(b.html);$("a.fbox").fancybox()})}var measureunit="inch";var isInIFrame=window.location!=window.parent.location?true:false;$(document).ready(function(){$(".sizeinch").css("cursor","pointer");$(".sizecm").css("cursor","pointer");$(".sizeinch").addClass("invert");$(".sizeinch").click(function(){if(measureunit=="inch"){}else{$(".sizecm").removeClass("invert");$(".sizeinch").addClass("invert");$(".measurements").each(function(){$(this).text(roundToHalf(parseFloat($(this).text())/2.54));measureunit="inch"})}});$(".sizecm").click(function(){if(measureunit=="cm"){}else{$(".sizeinch").removeClass("invert");$(".sizecm").addClass("invert");$(".measurements").each(function(){$(this).text(roundToHalf(parseFloat($(this).text())*2.54));measureunit="cm"})}});$("a.fbox").fancybox();if(isInIFrame){$("#container").css("width","800px");$("#slider img, #slider").css("width","800px");$("#slider img, #slider").css("height","500px");$("body").css("margin","0px");$(".product").css("margin","0px 17px 30px 0px");$(".product .image, .product .details").css("width","180px");$(".product .image").css("height","auto");$("#category-container").css("width","800px");$("#category-main").css("width","600px");$("#category-side").css("width","190px");$(".thumbs").css("display","none");$(".productm").css("margin","3px");$("#product .images .featured img").css("max-width","350px");$("#bannerlogo img").css("height","200px");$("#bannerlogo img").css("width","auto");$("#bannernav").css("top","0px");$("#storefront-bar").css("display","none")}})

Currency.format = 'money_with_currency_format';

var shopCurrency = 'AUD';
var cookieCurrency = Currency.cookie.read();
var previousCurrency = 'AUD';

// If there's no cookie.
if (cookieCurrency == null) {
  Currency.currentCurrency = shopCurrency;
}
else {
  Currency.convertAll(shopCurrency, cookieCurrency);
}

jQuery('.currencyitem').click(function() {
  var newCurrency = jQuery(this).text();
  Currency.convertAll(Currency.currentCurrency, newCurrency);
});

var original_selectCallback = window.selectCallback;
var selectCallback = function(variant, selector) {
  original_selectCallback(variant, selector);
  Currency.convertAll(shopCurrency, jQuery('[name=currencies]').val());
};


function ajax_restock() {
    $('.ajax-loader').addClass('show');
    $('#restock_notify').attr('disabled', true).addClass('btn-disabled');

    var data = $('#subscribe_form').serialize();
    $.ajax({
        url: 'http://spandex.blackmilkclothing.com/api/subscribe/add',
        type: 'POST',
        async: false,
        data: data,
        beforeSend: function () {
	        $('#subscribe_form').validate();
            valid = $('#subscribe_form').valid();
	        if (valid == true) {
    	        return true;
            } else {
		        $('.ajax-loader').removeClass('show');
		        $('#restock_notify').removeAttr('disabled').removeClass('btn-disabled');
		        return false;
            }
	},
        success: function (data, status, xhr) {
	        $('.ajax-loader').removeClass('show');
            $('#restock_notify').attr("disabled", false).removeClass('btn-disabled');
	        $('.modal-header h3').html('Thank you!');
      	    $('.modal-body').html('<p>We will notify you when this product gets restocked. :)</p>');
            $('.modal-footer').html('');
        }
    });

    return false;
}

$(document).ready(function () {
    /*$('#slider').cycle({
        fx:'scrollLeft' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
    });*/
    
    if ($('.bmsizes-bmxsmall-thumbs').length > 0) {
        var tagx = 'bmxsmall';
        featurethumbs(tagx);
        $('div.bmsizes-bmxsmall-thumbs').html('<div id="facebooktags-'+tagx+'"></div>');
    }
    
    if ($('.bmsizes-bmsmall').length > 0) {
        var tags = 'bmsmall';
        featurethumbs(tags);
        $('div.bmsizes-bmsmall-thumbs').html('<div id="facebooktags-'+tags+'"></div>');
    }
    
    if ($('.bmsizes-bmmedium').length > 0) {
        var tagm = 'bmmedium';
        featurethumbs(tagm);
    	$('div.bmsizes-bmmedium-thumbs').html('<div id="facebooktags-'+tagm+'"></div>');
    }
    
    if ($('.bmsizes-bmlarge').length > 0) {
        var tagl = 'bmlarge';
        featurethumbs(tagl);
    	$('div.bmsizes-bmlarge-thumbs').html('<div id="facebooktags-'+tagl+'"></div>');
    }
    
    $("a.arestock").fancybox();
    
    $('a.arestock').on('click', function () {
        var link_data = $(this);
        $('#subscribe_form .product_id').val(link_data.data('productid'));
        $('#subscribe_form .variant_id').val(link_data.data('variantid'));
        $('#subscribe_form .ptitle').val(link_data.data('ptitle'));
        $('#subscribe_form .topic').val('restock_' + link_data.data('productid') + '_' + link_data.data('variantid')),
        $('.modal-header h3').html('RESTOCK NOTIFICATION FOR ' + link_data.data('ptitle'));
    });
    
    $('#restock_notify').on('click', function () {
        ajax_restock();
    });

});